﻿using AutoMapper;
using B.S.Base.ErrorCode;
using B.S.Base.Infrastructrue;
using B.S.Base.Moain.Entities;
using B.S.Base.Write.Api.Application.Command.Shops;
using MediatR;

namespace B.S.Base.Write.Api.Application.CommandHander.Shops
{
    public class AddShopCommandHander : IRequestHandler<AddShopCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Shop> shop;
        private readonly IMapper mapper;

        public AddShopCommandHander(IBaseRepository<Shop> shop, IMapper mapper)
        {
            this.shop = shop;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(AddShopCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var user = mapper.Map<Shop>(request);
            var r = shop.Add(user).Result;
            if (r > 0)
            {
                result.Code = APIEnum.操作成功;
                result.Msg = "添加成功";
                result.data = r;
            }
            else
            {
                result.Code = APIEnum.操作失败;
                result.Msg = "添加失败";
                result.data = -1;
            }
            return Task.FromResult(result);
        }
    }

}
